﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;

using agvcontroller.Common;
using agvcsimulator.Driver;
using agvcontroller.Log;
using agvcontroller.Common.DataBase;

namespace agvcsimulator.Module
{
    public class CChargerSim
    {
        public int ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public bool Use
        {
            get;
            set;
        }

        public int ChargerNodeID
        {
            get;
            set;
        }

        public IPAddress TerminalIP
        {
            get;
            set;
        }

        public IPAddress AGVCIP
        {
            get;
            set;
        }

        public int Port
        {
            get;
            set;
        }

        Logger Log;

        CAGVCDB AGVCDB;

         public CChargerSim(string name)
        {
            Log = new Logger(name);

            this.Name = name;

            AGVCDB = CAGVCDB.getInstance();

            //SocketDirver = new CTerminalSimSocketDriver(AGVCIP, Port);
        }
    }

    public class CChargerSimDic : Dictionary<int , CChargerSim>
    {
        CAGVCDB AGVCDB;
        Logger Log;

        private static CChargerSimDic uniqueInstance;

        public static CChargerSimDic getInstance()
        {
            if (uniqueInstance == null)
            {
                uniqueInstance = new CChargerSimDic();
            }
            return uniqueInstance;
        }

        public CChargerSimDic()
        {
            Log = new Logger("ChargerSimDic");
            AGVCDB = CAGVCDB.getInstance();

            InitModule();
        }

        public int InitModule()
        {
            try
            {
                DataTable table = AGVCDB.AGVCDataSet.Tables["ChargerList"];
                CChargerSim terminalSim;

                foreach (DataRow row in table.Rows)
                {
                    terminalSim = new CChargerSim(Convert.ToString(row["Name"]));
                    //terminalSim.ID = Convert.ToInt32(row["ID"]);
                    //terminalSim.CommType = Convert.ToString(row["CommType"]);
                    //terminalSim.EjectType = Convert.ToString(row["EjectType"]);
                    //terminalSim.GuideType = Convert.ToString(row["GuideType"]);
                    //terminalSim.BatteryLowLevel = Convert.ToInt32(row["BatteryLowLevel"]);
                    //terminalSim.CanForward = Convert.ToBoolean(row["CanForward"]);
                    //terminalSim.CanBackward = Convert.ToBoolean(row["CanBackward"]);
                    //terminalSim.CanLeft = Convert.ToBoolean(row["CanLeft"]);
                    //terminalSim.CanRight = Convert.ToBoolean(row["CanRight"]);
                    //terminalSim.CanSideLeft = Convert.ToBoolean(row["CanSideLeft"]);
                    //terminalSim.CanSideRight = Convert.ToBoolean(row["CanSideRight"]);
                    //terminalSim.Port = Convert.ToInt32(row["Port"]);
                    //terminalSim.AGVCIP = IPAddress.Parse(Convert.ToString(row["AGVCIP"]));
                    //terminalSim.VehicleIP = IPAddress.Parse(Convert.ToString(row["VehicleIP"]));

                    //this.Add(terminalSim.ID, terminalSim);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(string.Format("{0},{1}", ex.Message, ex.StackTrace));
                return CReturnCode.ERR_DATA_ACQUIRING_FAIL;
            }

            return CReturnCode.SUCCESS;
        }

        public int GetDataFromDB()
        {

            return CReturnCode.SUCCESS;

        }
    }
}
